<template>
		<view>
			<view class="end-title">
			　　<view @tap="chenked(0)" :class="{btna:btnnum == 0}">未盘({{wptotal}})</view>
			  　<view @tap="chenked(1)" :class="{btna:btnnum == 1}">已盘({{yptotal}})</view>
			</view>
			<view class="end-cont" :class="{dis:btnnum == 0}">
				<view class="headItem">
					<view class="item-link item-content">
						<!--<div class="item-media"></div>-->
						<view class="item-inner">
							<view class="item-text">
								<view class="ispan">小计：{{wptotal}}</view>
							</view>
						</view>
					</view>
				</view>
				<scroll-view class="scroll-view_H" scroll-x="true" scroll-y="true" v-for="(item,index) in productList" :key="index">
					<view class="Item">
						<view class="item-link item-content">
							<!--<div class="item-media"></div>-->
							<view class="item-inner"  @tap="stockWpAsset($event)" :data-Id="item.AssetId">
								<view class="item-text">
									<view  class="ispan" style="background-color:#6D8AF9;width: 60rpx;color:#FFFFFF;padding: 0 10rpx;margin-right: 10rpx;">待盘</view>
									<view class="ispan">资产编码：{{item.Code}}</view>
								</view>
								<view class="item-text">
									<view class="ispan">盘点人：{{loginName}}</view>
								</view>
								<view class="item-text">
									<view class="ispan">盘点日期：</view>
								</view>
								<view class="item-text">
									<view class="ispan">所在城市：</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
								
			</view>
			<view class="end-cont" :class="{dis:btnnum == 1}">
				<view class="headItem">
					<view class="item-link item-content">
						<!--<div class="item-media"></div>-->
						<view class="item-inner">
							<view class="item-text">
								<view class="ispan">小计：{{yptotal}}</view>
							</view>
						</view>
					</view>
				</view>
				<scroll-view class="scroll-view_H" scroll-x="true" scroll-y="true" v-for="(item,index) in ypproductList" :key="index">
						<view class="Item">
							<view class="item-link item-content">
								<!--<div class="item-media"></div>-->
								<view class="item-inner" @tap="stockAsset($event)" :data-Id="item.AssetId">
									<view class="item-text">
										<view  class="ispan" style="background-color:#BEBEBE;width: 60rpx;color:#FFFFFF;padding: 0 10rpx;margin-right: 10rpx;">已盘</view>
										<view class="ispan">资产编码：{{item.Code}}</view>
									</view>
									<view class="item-text">
										<view class="ispan">盘点人：{{loginName}}</view>
									</view>
									<view class="item-text">
										<view class="ispan">盘点日期：{{item.盘点日期}}</view>
									</view>
									<view class="item-text">
										<view class="ispan">所在城市：{{currAddress}}</view>
									</view>
								</view>
							</view>
						</view>
					</scroll-view>
			</view>
			</view>
		

</template>

<script>
	var self;
	export default {
		data() {
			return {
				model: {},
				productList: [],
				ypproductList: [],
				loginName: "",
				currAddress: "上海市",
				DocumentId: "",
				btnnum: 0,
				wptotal:0,
				yptotal:0,
				IsFinished:false,
				searchtext:""
			}

		},
		onNavigationBarButtonTap(val) {
			if(val.index==1){//搜索
				uni.navigateTo({
					url: '../stock/StockDocumentDetailSearch?Search=' + this.searchtext,
				});
				}
			if(val.index==0){
				   // 只允许通过相机扫码
				   if(this.IsFinished){
					   uni.showToast({
						icon: 'none',
						title: '盘点单已结束'
					   });
				   }else{
				   uni.scanCode({
					onlyFromCamera: true,
					success: function(qrres) {
							var mymodel = JSON.parse(qrres.result);
							var myDocumentId=this.DocumentId;
							console.log(mymodel.唯一标识);
							uni.request({
								url: 'http://120.77.180.230:8055/ServerAPI/Document_Get_Detail_List', //查询明细
								data: {
									DocumentId: myDocumentId,
									pageSize: 50,
									_r: "813efd8e-1ac0-4cc8-8c35-3d502f680353",
									AssetId: mymodel.唯一标识,
								},
								success: res => {
									if (res.data.successed) {
										if (res.data.total == 0) {
											uni.showToast({
												icon: 'none',
												title: '扫描资产不在盘点单内'
											});
										} else {
											
											uni.navigateTo({
												url: '../stock/StockDocumentAsset?AssetId=' + mymodel.唯一标识
											});
											
										}
									}else{
										uni.showToast({
											icon: 'none',
											title: res.data.responseState
										});
									}
				   
								},
								fail: () => {
				   
									uni.showToast({
										icon: 'none',
										title: '网络异常,请稍后重试'
									});
								}
							});
					}
				   });
				   }
			   }
		    },
		onLoad(options) {
			//首先将实例指针保存到全局变量 self 方便在异步请求中访问实例本身
			
			self = this;
			this.searchtext=options.Search;
			this.DocumentId = options.id;
			
			
			uni.setStorage({
				key: 'DocumentId',
				data: options.id
			});
			uni.showLoading({
			    title: '加载中'
			});
			uni.getStorage({
				key: 'userinfo',
				success: function(newres) {
					const listdata = newres.data;
					self.loginName = listdata.LoginName;
				}
			});
			/*
			uni.getLocation({
				type: 'wgs84',
				success: function(res) {
					var point = new plus.maps.Point(res.longitude, res.latitude);
					plus.maps.Map.reverseGeocode(
						point, {},
						function(event) {
							var address = event.address; // 转换后的地理位置
							var point = event.coord; // 转换后的坐标信息
							var coordType = event.coordType; // 转换后的坐标系类型
							var reg = /.+?(省|市|自治区|自治州|县)/g;
							//var reg = /.+?(省|市|自治区|自治州|县)/g;
							self.currAddress = address.match(reg)[0];
						},
						function(e) {}
					);
				}
			});
			*/
			uni.request({
				url: 'http://120.77.180.230:8055/ServerAPI/Document_Get_Model', //仅为示例，并非真实接口地址。
				data: {
					Id: options.id,
					pageSize: 9999,
					_r: "813efd8e-1ac0-4cc8-8c35-3d502f680353"					
				},
				success: res => {

					if (res.data.successed == true) {
						 this.IsFinished=res.data.model.IsFinished;
						this.model = res.data.model;
						 uni.setNavigationBarTitle({
						            title:res.data.model.Name
						        });
					}
				},
				fail: () => {

					uni.showToast({
						icon: 'none',
						title: '网络异常,请稍后重试'
					});
				}
			});

			uni.request({
				url: 'http://120.77.180.230:8055/ServerAPI/Document_Get_Detail_List', //仅为示例，并非真实接口地址。
				data: {
					DocumentId: options.id,
					pageSize: 50,
					_r: "813efd8e-1ac0-4cc8-8c35-3d502f680353",
					盘点状态:"未盘",
					"-Code":this.searchtext
				},
				success: res => {
					if (res.data.successed == true) {
						
						this.wptotal=res.data.total;
						var mydata = [];
						var newrows = JSON.parse(res.data.rows);
						for (var i = 0; i < newrows.length; i++) {
							var valueRows = newrows[i].toString().split(',');
							var datarow = {};
							for (var j = 0; j < res.data.columns.length; j++) {
								//alert(_data.columns[j].字段名)
								datarow[res.data.columns[j].字段名] = valueRows[j];
							}
							mydata.push(datarow);
						}
						this.productList = mydata;
						
					}

				},
				fail: () => {

					uni.showToast({
						icon: 'none',
						title: '网络异常,请稍后重试'
					});
				}
			});

			uni.request({
				url: 'http://120.77.180.230:8055/ServerAPI/Document_Get_Detail_List', //仅为示例，并非真实接口地址。
				data: {
					DocumentId: options.id,
					pageSize: 50,
					_r: "813efd8e-1ac0-4cc8-8c35-3d502f680353",
					盘点状态:"已盘",
					"-Code":this.searchtext
				},
				success: res => {
					if (res.data.successed == true) {
						this.yptotal=res.data.total;
						var mydata = [];
						var newrows = JSON.parse(res.data.rows);
						for (var i = 0; i < newrows.length; i++) {
							var valueRows = newrows[i].toString().split(',');
							var datarow = {};
							for (var j = 0; j < res.data.columns.length; j++) {
								//alert(_data.columns[j].字段名)
								datarow[res.data.columns[j].字段名] = valueRows[j];
							}
							mydata.push(datarow);
						}
						this.ypproductList = mydata;
						if(res.data.total==0 && this.wptotal==0){
							uni.showToast({
								icon: 'none',
								title: '暂无数据'
							});
						}
					}
					uni.hideLoading();
			
				},
				fail: () => {
			
					uni.showToast({
						icon: 'none',
						title: '网络异常,请稍后重试'
					});
				}
			});
			
			
		},
onShow() {
	uni.request({
		url: 'http://120.77.180.230:8055/ServerAPI/Document_Get_Detail_List', //仅为示例，并非真实接口地址。
		data: {
			DocumentId:this.DocumentId ,
			pageSize: 50,
			_r: "813efd8e-1ac0-4cc8-8c35-3d502f680353",
			盘点状态:"未盘",
			"-Code":this.searchtext
		},
		success: res => {
			if (res.data.successed == true) {
				
				this.wptotal=res.data.total;
				var mydata = [];
				var newrows = JSON.parse(res.data.rows);
				for (var i = 0; i < newrows.length; i++) {
					var valueRows = newrows[i].toString().split(',');
					var datarow = {};
					for (var j = 0; j < res.data.columns.length; j++) {
						//alert(_data.columns[j].字段名)
						datarow[res.data.columns[j].字段名] = valueRows[j];
					}
					mydata.push(datarow);
				}
				this.productList = mydata;
				
			}
	
		},
		fail: () => {
	
			uni.showToast({
				icon: 'none',
				title: '网络异常,请稍后重试'
			});
		}
	});
	
	uni.request({
		url: 'http://120.77.180.230:8055/ServerAPI/Document_Get_Detail_List', //仅为示例，并非真实接口地址。
		data: {
			DocumentId: this.DocumentId,
			pageSize: 50,
			_r: "813efd8e-1ac0-4cc8-8c35-3d502f680353",
			盘点状态:"已盘",
			"-Code":this.searchtext
		},
		success: res => {
			if (res.data.successed == true) {
				this.yptotal=res.data.total;
				var mydata = [];
				var newrows = JSON.parse(res.data.rows);
				for (var i = 0; i < newrows.length; i++) {
					var valueRows = newrows[i].toString().split(',');
					var datarow = {};
					for (var j = 0; j < res.data.columns.length; j++) {
						//alert(_data.columns[j].字段名)
						datarow[res.data.columns[j].字段名] = valueRows[j];
					}
					mydata.push(datarow);
				}
				this.ypproductList = mydata;
				if(res.data.total==0 && this.wptotal==0){
					uni.showToast({
						icon: 'none',
						title: '暂无数据'
					});
				}
			}
	
		},
		fail: () => {
	
			uni.showToast({
				icon: 'none',
				title: '网络异常,请稍后重试'
			});
		}
	});
	
	
	},
		methods: {
			stockAsset(e) {
				uni.navigateTo({
					url: '../stock/StockDocumentAsset?AssetId=' + e.currentTarget.dataset.id,
				});				
			},
			stockWpAsset(e) {
				console.log("12");
				uni.navigateTo({
					url: '../stock/StockDocumentAsset',
				});				
			},
			stocksys() {
				// 只允许通过相机扫码
				uni.scanCode({
					onlyFromCamera: true,
					success: function(qrres) {
							var mymodel = JSON.parse(qrres.result);
							var myDocumentId=this.DocumentId;
							console.log(mymodel.唯一标识);
							uni.request({
								url: 'http://120.77.180.230:8055/ServerAPI/Document_Get_Detail_List', //查询明细
								data: {
									DocumentId: myDocumentId,
									pageSize: 50,
									_r: "813efd8e-1ac0-4cc8-8c35-3d502f680353",
									AssetId: mymodel.唯一标识,
								},
								success: res => {
									if (res.data.successed) {
										if (res.data.total == 0) {
											uni.showToast({
												icon: 'none',
												title: '扫描资产不在盘点单内'
											});
										} else {
											
											uni.navigateTo({
												url: '../stock/StockDocumentAsset?AssetId=' + mymodel.唯一标识
											});
											
										}
									}else{
										uni.showToast({
											icon: 'none',
											title: res.data.responseState
										});
									}

								},
								fail: () => {

									uni.showToast({
										icon: 'none',
										title: '网络异常,请稍后重试'
									});
								}
							});


							//productList
						

					}
				});
			},
		chenked(type) {
						this.btnnum = type
						
					},
		},
		 onPullDownRefresh() {
		        console.log('refresh');
		        setTimeout(function () {
		            uni.stopPullDownRefresh();
		        }, 1000);
		    }

	}
</script>

<style>
	
	.Item {
		border: 1px solid #e6e6e6;
		overflow: hidden;
		padding-bottom: 16rpx;
		border-radius: 16rpx;
		background: #fff;
		margin-bottom: 10rpx;
	}
	.headItem{
		border: 1px solid #e6e6e6;
		overflow: hidden;
		padding-bottom: 5rpx;
		background: #fff;
		margin-bottom: 10rpx;
		border-right: 0px;
		border-left: 0px;
	}

	.item-text .span {
		display: inline-block;
		width: 45%;
		float: right;
	}

	.item-text .ispan {
		font-style: normal;
		width: 53%;
		display: inline-block;
	}

	.item-content {
		padding-left: 0;
	}

	.item-title .ispan {
		font-size: 30rpx;
		width: auto;
		height: auto;
	}

	.item-title {
		font-size: 40rpx;
		color: #454545;
		width: 100%;
	}

	.item-title-row {
		background-image: none;
		padding-right: 0px;
	}

	.item-link .item-inner {
		padding-right: 0;
	}

	.page-content {
		padding-top: 0;
	}

	.item-text {
		font-size: 30rpx;
		color: #454545;
		margin: 20rpx 0;
		padding-left: 20rpx;
		line-height: 1.2;
	}
	.headItem .item-text {
		font-size: 30rpx;
		color: #7f7f7f;
		margin: 14rpx 0;
		padding-left: 20rpx;
		line-height: 1.2;
	}

	.btext {
		font-weight: normal;
		color: #F79D63;
	}

	.scroll-view_H {
		white-space: nowrap;

		.scroll-view-item_H {
			display: inline-block;
			width: 100%;
			height: 100px;
		}

		.uni-bg-red {
			background: red;
		}

		.uni-bg-green {
			background: green;
		}

		.uni-bg-blue {
			background: blue;
		}
	}
		
		/* 将三个内容view的display设置为none(隐藏) */
		    .end-title{
		        display: flex;
				margin: 0px 20rpx;
				border: 1px solid #e6e6e6;
				border-top: 0px solid #e6e6e6;
				border-left: 0px solid #e6e6e6;
				border-right: 0px solid #e6e6e6;
				border-bottom: 0px solid #e6e6e6;
		    }
		    .end-title view{
		        flex-grow: 1;
		        text-align: center;
				height: 80rpx;
				line-height: 80rpx;
				font-size: 30rpx;
				font-weight: bold;
		    }
		    .end-cont{
		        display: none;
				margin: 0 20rpx;
		    }
		    .btna{
		       color:#0A98D5;
			   border-bottom: 2px #0A98D5 solid;
			   
		    }
		    .dis{
		        display: block;
		    } 
	
</style>
